package com.yoda.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yoda.youdianda.common.ErrorResult;
import com.yoda.youdianda.common.R;
import com.yoda.youdianda.common.RC;
import com.yoda.youdianda.domain.SysDict;
import com.yoda.youdianda.domain.SysMenu;
import com.yoda.youdianda.dto.MenuDto;
import com.yoda.youdianda.exception.BusinessException;
import com.yoda.youdianda.mapper.SysMenuMapper;
import com.yoda.youdianda.service.SysMenuService;
import com.yoda.youdianda.util.Util;
import com.yoda.youdianda.vo.MenuDetailVo;
import com.yoda.youdianda.vo.MenuVo;
import com.yoda.youdianda.vo.WholeMenuVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author shawn
 * @date 2022年 11月 13日 9:40
 */

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper mapper;

    @Override
    public List<SysMenu> getAll() {
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMenu::getDelFlag,0);
        return mapper.selectList(wrapper);
    }

    /**
     * 得到所有父项(包含子项)vo
     *
     * @return {@link List}<{@link WholeMenuVo}>
     */
    @Override
    public List<MenuDetailVo> getAllMenu() {
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMenu::getDelFlag,0);
        List<SysMenu> menus = mapper.selectList(wrapper);
        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(0);
        List<MenuDetailVo> vos = Util.getStructuredDirectory(menus, sysMenu);
        //过滤掉所有子项
        return vos;
    }

    /**
     * 得到菜单信息
     *
     * @param id id
     * @return {@link MenuVo}
     */
    @Override
    public MenuVo getMenuInfo(String id) {
        SimpleDateFormat format = new SimpleDateFormat(RC.GENERAL_DATE_FORMAT);
        SysMenu menu = mapper.selectById(id);
        return MenuVo.init(menu, format);
    }

    /**
     * 插入菜单
     *
     * @param dto dto
     * @return {@link R}<{@link String}>
     */
    @Override
    public R<String> insertMenu(MenuDto dto) {
        SysMenu sysMenu = MenuDto.init(dto);
        int count = mapper.insert(sysMenu);
        return count > 0 ? R.success("添加成功!") : R.error("添加失败!");
    }

    /**
     * 删除菜单
     *
     * @param id id
     * @return {@link R}<{@link String}>
     */
    @Override
    public R<String> deleteMenu(String id) {
        int count = mapper.deleteById(id);
        return count > 0 ? R.success("删除成功!") : R.error("删除失败!");
    }

    /**
     * 更新通过id
     *
     * @param dto dto
     * @return {@link R}<{@link String}>
     */
    @Override
    public R<String> updateById(MenuDto dto) {
        SysMenu sysMenu = MenuDto.init(dto);
        int count = mapper.updateById(sysMenu);
        return count > 0 ? R.success("修改成功!") : R.error("修改失败!");
    }

    /**
     * 根据角色id,查询所有菜单id
     *
     * @param roles_id 角色id
     * @return {@link List}<{@link String}>
     */
    @Override
    public List<SysMenu> getAllByIds(List<String> roles_id) {
        if (Objects.isNull(roles_id) || roles_id.size() == 0) {
            throw new BusinessException(ErrorResult.paramNullError());
        }
        //根据角色id查询所有角色菜单
        List<String> menuIds = mapper.selectMenuIdByRoleIds(roles_id);
        //根据菜单id,查找所有菜单
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysMenu::getId, menuIds);
        return mapper.selectList(wrapper);
    }

    /**
     * 根据菜单ids集合查询所有
     *
     * @param menuIds 菜单id
     * @return {@link List}<{@link SysMenu}>
     */
    @Override
    public List<SysMenu> getAllByMenuIds(List<String> menuIds) {
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Objects.nonNull(menuIds)||menuIds.size()>0,SysMenu::getId,menuIds);
        return mapper.selectList(wrapper);
    }
}
